﻿/**
 * Default.aspx.cs
 * 
 * AUTHOR:
 * Zlatko Michailov (http://blogs.msdn.com/esql)
 * 
 * PURPOSE:
 * Main view for Microsoft.Samples.Data.eSqlBlast.WebShell.
 * 
 * COPYRIGHT:
 * (c) Microsoft Corp., 2006-2007
 */



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;


namespace Microsoft.Samples.Data.eSqlBlast.WebShell
{
    public partial class _Default : System.Web.UI.Page
    {
        protected enum ViewIndex
        {
            Connection,
            Query,
            Results,
            Advanced,
        }

        protected readonly string[]ConnectionModels = 
        {
            "Aruba",
            "NorthwindEF",
            "AdventureWorks"
        };

        protected const string ProviderConnectionStringTemplate = @"Server=.; Database={0}; Integrated Security=true; Connection Timeout=5; MultipleActiveResultSets=true;";
        protected const string EntityConnectionStringTemplate = @"Provider=System.Data.SqlClient; Metadata=|DataDirectory|\Models\{0}; Provider Connection String='{1}';";
        protected const string ModelDefinitionFileTemplate = @"~\App_Data\Models\{0}\{0}.csdl";


        protected void ViewConnection(object sender, EventArgs e)
        {
            this.MainMultiView.ActiveViewIndex = (int)ViewIndex.Connection;
        }

        protected void ViewQuery(object sender, EventArgs e)
        {
            this.MainMultiView.ActiveViewIndex = (int)ViewIndex.Query;
        }

        protected void ViewResults(object sender, EventArgs e)
        {
            this.MainMultiView.ActiveViewIndex = (int)ViewIndex.Results;
        }

        protected void ViewAdvanced(object sender, EventArgs e)
        {
            this.Response.Redirect("~/advanced");
        }

        protected void Model_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = this.Model.SelectedIndex;

            if (0 <= i)
            {
                string providerConnectionString = string.Format(ProviderConnectionStringTemplate, ConnectionModels[i]);
                string entityConnectionString = string.Format(EntityConnectionStringTemplate, ConnectionModels[i], providerConnectionString);
                this.EntityConnectionString.Text = entityConnectionString;

                this.ModelDefinition.DocumentSource = string.Format(ModelDefinitionFileTemplate, ConnectionModels[i]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (this.MainMultiView.ActiveViewIndex)
            {
                case (int)ViewIndex.Query:
                    // If the request is a postback from the Query view, process the incoming query text.
                    if (this.IsPostBack)
                    {
                        StringWriter output = new StringWriter();

                        Microsoft.Samples.Data.eSqlBlast.Session blast = new Session();
                        blast.ResultVisitor = new XmlVisitor(output, null, this.FullExceptions.Checked);
                        blast.Connect(this.EntityConnectionString.Text);
                        blast.Execute(this.QueryText.Text);
                        blast.Disconnect();

                        this.Results.DocumentContent = output.ToString();
                        this.ViewResults(null, null);
                    }
                    break;
            }
        }

    }
}
